#!/bin/bash
# ### By FlyShine  ###
# ### 2011年7月15日 ###
function GetIniSection()
{
  local iniFile="$1"
  local sectionName="$2"
  if [ -f "$iniFile" ]; then
	  sectionValue="`awk -v key="$sectionName" -v RS='\\\\[[^\n]*]' 'v=="["key"]";{v=RT}' "$iniFile"`"
	  echo "$sectionValue"
  fi
}
function GetIniField()
{
	local sectionValue="$1"
	local fieldName="$2"
	fieldLine="`echo "$sectionValue" | grep "^$fieldName"`"
	if [ "$fieldLine" != "" ]; then
		value="`echo "$fieldLine" | awk -F '=' '{print $2}'`";
		echo "$value";
	fi
}
# ### execute from here ###
mode="$1" # "section" "field"
inifile="$2"
sect="$3"
field="$4"
targetValue=
case "$mode" in
section)
	targetSec="`GetIniSection "$inifile" "$sect"`"
	echo "$targetSec"
  ;;
field)
	targetSec="`GetIniSection "$inifile" "$sect"`"
	if [ "$targetSec" != "" ]; then
		targetValue="`GetIniField "$targetSec"  "$field"`"
	fi
	targetValue="`echo "$targetValue" | tr -d '"'`"
	echo $targetValue
  ;;
*)
	echo "" 
  ;;
esac
